# coding=utf8

from pyjvm.instructions.common import Instruction
from pyjvm.runtime.thread import Frame
from pyjvm.utils.jprimitive import *


def convert_inst_type(name, j_primitive):
    def execute(self, frame: Frame):
        stack = frame.operand_stack
        val = stack.pop()
        stack.push(j_primitive(val))

    return type(name, (Instruction,), dict(execute=execute))


class I2L(Instruction):  # 0x85
    """
    int  to  long
    """
    pass


I2F = convert_inst_type('I2F', j_float)
I2D = convert_inst_type('I2D', j_double)
L2I = convert_inst_type('L2I', j_int)
L2F = convert_inst_type('L2F', j_float)
L2D = convert_inst_type('L2D', j_double)
F2I = convert_inst_type('F2I', lambda val: int(j_float(val)))
F2L = convert_inst_type('F2L', lambda val: int(j_float(val)))
F2D = convert_inst_type('F2D', j_double)
D2I = convert_inst_type('D2I', d2i)
D2L = convert_inst_type('D2L', lambda val: int(j_double(val)))
D2F = convert_inst_type('D2F', j_float)
I2B = convert_inst_type('I2B', j_byte)
I2C = convert_inst_type('I2C', j_char)
I2S = convert_inst_type('I2S', j_short)

__all__ = ['I2B', 'I2C', 'I2D', 'I2F', 'I2L', 'I2S',
           'L2D', 'L2F', 'L2I', 'F2D', 'F2I', 'F2L',
           'D2F', 'D2I', 'D2L']
